﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TT.Logic.Entities
{
    /// <summary>
    /// Represents a single match/game
    /// </summary>
    public class Match
    {
        public int Id { get; set; }
        public DateTime? CompletedOn { get; set; }
        public int HomeScore { get; set; }
        public int AwayScore { get; set; }

        /// <summary>
        /// Return number of current match within round that match belongs to.
        /// </summary>
        public int MatchInRoundNumber { get; set; }        

        public virtual Round Round { get; set; }
        public virtual Player HomePlayer1 { get; set; }
        public virtual Player AwayPlayer1 { get; set; }

        //TODO: doubles not implemented yet.
        public virtual Player HomePlayer2 { get; set; }
        public virtual Player AwayPlayer2 { get; set; }

        /// <summary>
        /// Indicates whether match is read to be played - players are assigned to the match 
        /// and match is not finished yet
        /// </summary>
        public bool IsReadyToPlay {
            get
            {
                return HomePlayer1 != null && AwayPlayer1 != null && !IsFinished();
            }

        }

        /// <summary>
        /// returns boolean value indicating whether game is finished
        /// </summary>        
        public bool IsFinished(){
            bool isFinished = false;
            if ((HomeScore < 20 && AwayScore == 21) ||
                (AwayScore < 20 && HomeScore == 21) ||
                (HomeScore >= 20 && AwayScore >= 20 &&
                (HomeScore - AwayScore == 2 || AwayScore - HomeScore == 2)))
            {
                isFinished = true;
            }

            return isFinished;
        }

        /// <summary>
        /// Returns player that won the match
        /// </summary>        
        public Player GetWinner() {
            if (!IsFinished()) {
                throw new Exception("Match needs to be finished to have a winner");
            }

            return HomeScore > AwayScore ? HomePlayer1 : AwayPlayer1;
        }

        /// <summary>
        /// returns home match title
        /// </summary>        
        public string GetHomeTitle() {
            if (HomePlayer1 == null) {
                //var previousMatched = GetPreviousMatches(Round.RoundNumber, MatchInRoundNumber);
                return "-----";                    
            }
            return HomePlayer1.FirstName;
        }

        /// <summary>
        /// returns away match title
        /// </summary>        
        public string GetAwayTitle()
        {
            if (AwayPlayer1 == null)
            {
                //var previousMatched = GetPreviousMatches(Round.RoundNumber, MatchInRoundNumber);
                return "-----";                  
            }
            return AwayPlayer1.FirstName;
        }

        /// <summary>
        /// Returna a pair of match numbers in PREVIOUS round that are previous matches for the current match
        /// </summary>
        public Tuple<int, int> GetPreviousMatches(int roundNumber, int matchInRound)
        {
            //TODO: this does not take BYES into account fix it.
            if (roundNumber == 1)
            {
                return null;
            }
            if (roundNumber > 1)
            {
                if (matchInRound == 0)
                {
                    return new Tuple<int, int>(0, 1);
                }
                else
                {
                    return new Tuple<int, int>(matchInRound * 2, (matchInRound * 2) + 1);
                }
            }

            return null;
        }
    }
}
